<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="box-card">
        <el-tabs v-model="activeName">
          <el-tab-pane label="用户管理" name="first">
            <!-- 添加 -->
            <el-button size="mini" type="primary" icon="el-icon-plus" @click="dialogVisible=true">新增角色</el-button>
            <el-table
              :data="tableData"
              style="width: 100%;margin:15px 0;"
            >
              <el-table-column
                label="序号"
                width="50"
                type="index"
              />
              <el-table-column
                prop="name"
                label="角色名"
                width="180"
                sortable
              />
              <el-table-column
                prop="description"
                label="描述"
                sortable
              />
              <el-table-column
                prop="address"
                label="操作"
              >
                <template slot-scope="{row}">
                  <el-button type="text">分配权限</el-button>
                  <el-button type="text" @click="modify(row)">修改</el-button>
                  <el-button type="text">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="配置管理" name="second">
            <el-row>
              <el-col :offset="3" :span="8">
                <el-form ref="form" label-width="80px">
                  <el-form-item label="企业名称">
                    <el-input v-model="form.name" disabled />
                  </el-form-item>
                  <el-form-item label="公司地址">
                    <el-input v-model="form.companyAddress" disabled />
                  </el-form-item>
                  <el-form-item label="公用电话">
                    <el-input v-model="form.companyPhone" disabled />
                  </el-form-item>
                  <el-form-item label="邮箱">
                    <el-input v-model="form.mailbox" disabled />
                  </el-form-item>
                  <el-form-item label="备注">
                    <el-input v-model="form.remarks" disabled />
                  </el-form-item>
                </el-form>
              </el-col>
            </el-row>
          </el-tab-pane>
          <el-pagination
            :current-page="queryData.page"
            :page-sizes="[2, 5, 10, 15]"
            :page-size="queryData.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-tabs>

      </el-card>
      <!-- 弹层 -->
      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="50%"
        @close="dialogClose"
      >
        <el-form ref="addform" :model="addform" :rules="addRoleRules" label-width="80px">
          <el-form-item prop="name" label="角色名称">
            <el-input v-model="addform.name" />
          </el-form-item>
          <el-form-item prop="description" label="角色描述">
            <el-input v-model="addform.description" />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirmAdd">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getcompanyDetail } from '@/api/company'
import { getAllrolesList, AddRoles, EditRoles } from '@/api/roles'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      activeName: 'second',
      form: {

      },
      tableData: [],
      queryData: {
        page: 1,
        pagesize: 2
      },
      total: 0,
      dialogVisible: false,
      addform: {
        name: '',
        description: ''
      },
      addRoleRules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },

  created() {
    this.getCompanyDetail()
    this.loadRolesList()
  },

  methods: {
    async getCompanyDetail() {
      const res = await getcompanyDetail(this.companyId)
      console.log(res)
      this.form = res
    },
    async  loadRolesList() {
      const res = await getAllrolesList(this.queryData)
      console.log(res.rows)
      this.total = res.total
      this.tableData = res.rows
    },
    handleCurrentChange(val) {
      this.queryData.page = val
      this.loadRolesList()
    },
    handleSizeChange(val) {
      this.queryData.pagesize = val
      this.loadRolesList()
    },
    // 提交添加
    async  confirmAdd() {
      try {
        await this.$refs.addform.validate()
        if (this.addform.id) {
          console.log('修改')
          await EditRoles(this.addform)
          this.$message.success('更新成功')
        } else {
          // 添加
          await AddRoles(this.addform)
          this.$message.success('添加成功')
        }
        this.dialogVisible = false
        this.loadRolesList()
      } catch (error) {
        console.log(error)
      }
    },
    // 修改
    modify(val) {
      console.log(val)
      this.dialogVisible = true
      this.addform = { ...val }
    },
    // 对话框关闭的时候
    dialogClose() {
      this.$refs.addform.resetFields()
      this.addform = {
        name: '',
        description: ''
      }
      console.log(1)
    }
  }
}
</script>

<style scoped lang='scss'>
::v-deep .el-tabs__header{
  .el-tabs__item {
    width: 140px;
    text-align: center;
    font-size: 16px;
  }
}
.el-input{
  font-size: 13px;
}
::v-deep .el-form-item__label{
  font-weight: 400;
}
</style>
